<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Feedback;
use api\wxapp\model\User;
class FeedbackController extends AdminBaseController
{
    public function index()
    {
        $list = Feedback::order('path')->paginate(10,false,
        [])->each(function($item, $key){
            if($item['pid']){
                $item['fname'] = Feedback::where('id',$item['pid'])->value('name');
                $item['name'] = '++'.$item['name'];
            }else{
                $item['fname'] = '顶级分类';
            }
            return $item;
        });
        $this->assign('list',$list);
        //$list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function add()
    {
        if($_POST){
            if(!$_POST['name']){
                $this->error('参数为空!!!!');
            }
            $path = Feedback::get($_POST['pid']);
            $id = Feedback::insertGetId($_POST);
            $data['path'] = $path['path'].','.$id;
            $data['id'] = $id;
            $res = Feedback::update($data);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $cate = Feedback::where('pid',0)->all();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function edit()
    {
        $params= $this->param();
        $info = Feedback::get($params['id']);
        if($_POST){
            if(!$_POST['name']){
                $this->error('参数为空!!!!');
            }
            if($_POST['pid'] !== $info['pid']){
                $path = Feedback::get($_POST['pid']);
                $_POST['path'] = $path['path'].','.$info['id'];
            }
            $res = Feedback::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $cate = Feedback::where('pid',0)->all();
        $this->assign('info',$info);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function delete()
    {
        $params= $this->param();
        $res = Feedback::destroy($params['id']);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    public function lst()
    {
        $params= $this->param();
        $where = array();
        if(isset($params['id'])){
            $idarr = Feedback::where('pid',$params['id'])->column('id');
            $where[] = ['feedback_id','in',$idarr];
        }
        $list = db::name('app_feedback_list')->where($where)->paginate(15,false,
        ['query'=>$params])->each(function($item, $key){
            $item['name'] = Feedback::where('id',$item['feedback_id'])->value('name');
            $item['nickname'] = User::where('id',$item['uid'])->value('nickname');
            $item['img'] = json_decode($item['img'],true);
            return $item;
        });
        $this->assign('list',$list);
        $list->appends($params);
        $this->assign('page', $list->render());
        return $this->fetch();
    }
}
